package kr.ltkblue;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class SendXmlData {
	public static void main(String[] args) throws Exception {
//		System.exit(0);
		String linesep ="\r\n";

		HttpURLConnection httpConn = null;

			String targetURL = "http://www.shinhan.com/common/hpe/jsp/callHpeServletService.jsp";
			URL url = new URL(targetURL);
			httpConn = (HttpURLConnection) url.openConnection();
//			httpConn.setRequestProperty("Cookie", "PHAROS_VISITOR=000000000140112406d953e7c0a86f96; JSESSIONID=n737RvyJLbSJSM3klSKvtQtyn6TdJ33Qh3GkqDZvJNX60n1pyhSl!-160686391; session.server=hpcwb1p.nex2hpe2; session.id=n737RvyJLbSJSM3klSKvtQtyn6TdJ33Qh3GkqDZvJNX60n1pyhSl!-160686391!1374677108441; PCID=13746771427515074469778");

			httpConn.setUseCaches(false);
			httpConn.setRequestMethod("POST");
			httpConn.setRequestProperty("Cache-Control", "no-cache");
			httpConn.setRequestProperty("Accept", "application/x-www-form-urlencoded; charset=UTF-8");
			httpConn.setRequestProperty("charset","utf-8");
//			httpConn.setRequestProperty("Content-Type","application/json;charset=utf-8");
			// httpConn.setRequestProperty("Cookie", "cookievalue="+ cookie);
			httpConn.setDoInput(true);
			httpConn.setDoOutput(true);

			String key = "task=sfg.hpe.task.branch.Branch&action=getAddList&documentUrl=/contents/about_bank/branch/internal01_type01.xml&idx=&plainXML=";
			String xmlData = "%3CSEARCH%20task%3D%22sfg.hpe.task.branch.Branch%22%20action%3D%22getAddList%22%20page%3D%222%22%20pageSize%3D%2210%22%3E%3CBRA_USE%20value%3D%220%22%2F%3E%3CBRA_ADDR1%20value%3D%22%EC%84%9C%EC%9A%B8%22%2F%3E%3CBRA_ADDR3%20value%3D%22%25%25%22%2F%3E%3CBRA_ADDR2%20value%3D%22%25%25%22%2F%3E%3C%2FSEARCH%3E";
			OutputStream          os   = null;
			os = httpConn.getOutputStream();
			os.write((key+xmlData.toString()).getBytes());
			os.flush();
//
//
//			PrintWriter pw = new PrintWriter(new OutputStreamWriter(httpConn.getOutputStream(), "utf-8"));
//
//			pw.write(urlParameters.toString());
//			pw.flush();
//			pw.close();

			// Get Response
			BufferedReader rd = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
			String line;

			StringBuffer sb = new StringBuffer();
			while ((line = rd.readLine()) != null) {
	//			System.out.println(line);
				sb.append(line);
				// response1.append('\r');
			}
			System.out.println(sb);
			httpConn.disconnect();
			rd.close();


	}

	public static String unicodeToString(String str){

		String uniCodeStr = String.format(str);
		String[] rawUnicode = uniCodeStr.split("\\");
		StringBuffer sb = new StringBuffer();
		int intVal = 0;
		char[] chVal;

		for(int i = 0; i < rawUnicode.length; i++ ) {
			chVal = rawUnicode[i].toCharArray();
			for(int j = 0; j < chVal.length; j++ ) {
				intVal = chVal[j];
				sb.append(chVal[j]);
			}
		}

		return sb.toString();
	}


	 public static String decode(String unicode)throws Exception {
	  StringBuffer str = new StringBuffer();

	  char ch = 0;
	  for( int i= unicode.indexOf("\\u"); i > -1; i = unicode.indexOf("\\u") ){
	   ch = (char)Integer.parseInt( unicode.substring( i + 2, i + 6 ) ,16);
	   str.append( unicode.substring(0, i) );
	   str.append( String.valueOf(ch) );
	   unicode = unicode.substring(i + 6);
	  }
	  str.append( unicode );

	  return str.toString();
	 }
	 
	public void sleep(int time) {
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
		}
	}

}
